#include "main.h"
#include "tim.h"
#include "scnsInclude.h"
#include "stm32h7xx_it.h"
#include "stm32h750xx.h"
#include "spi.h"

#define MEM_TEST
#define FLASH_TEST
#define SPI_FLASH_TEST
#define QSPI_FLASH_TEST
//#define SQRT_TEST

#if __has_include("image.h")

#include "image.h"

#endif

#ifdef MEM_TEST

static void memTest()
{
    extern uint32 __memset_acc_cnt,__memset_call_cnt,__memcpy_acc_cnt,__memcpy_call_cnt;
    hxPrintfFL("%u %u %u %u\n",__memset_acc_cnt,__memset_call_cnt,__memcpy_acc_cnt,__memcpy_call_cnt);
    const uint32_t basePriBak=__get_BASEPRI();
    __set_BASEPRI(1<<4);
    SCB_DisableDCache();
    scnsMallocMemTest("DTCM",SCNS_MALLOC_DTCM,scnsRand(),32*1024);
    __set_BASEPRI(basePriBak);
    scnsDelayMs(100);
    __set_BASEPRI(1<<4);
    scnsMallocMemTest("RAM1",SCNS_MALLOC_AXI,scnsRand(),32*1024);
    SCB_EnableDCache();
    __set_BASEPRI(basePriBak);
    hxPrintfFL("%u %u %u %u\n",__memset_acc_cnt,__memset_call_cnt,__memcpy_acc_cnt,__memcpy_call_cnt);
}

#endif
#ifdef SPI_FLASH_TEST

static void spiFlashTest()
{
    const ScnsStatus status=scnsW25qxxTest(SCNS_W25QXX_SPI,0,scnsRand(),80*4096,1);
    if(status!=SCNS_STATUS_OK){hxPrintfFL("%d:%s\n",status,scnsStatusGetString(status));}
}

#endif
#ifdef FLASH_TEST

static void flashTest()
{
    const uint32_t basePriBak=__get_BASEPRI();
    __set_BASEPRI(1<<4);
    scnsMemTestRo("flash",(const uint32*)0x08000000,0x00020000,20);
    __set_BASEPRI(basePriBak);
}

#endif
#ifdef QSPI_FLASH_TEST

static void qspiFlashTest()
{
    const uint32_t basePriBak=__get_BASEPRI();
    __set_BASEPRI(1<<4);
    const ScnsStatus status1=scnsW25qxxTest(SCNS_W25QXX_QSPI,0,scnsRand(),80*4096,1);
    const ScnsStatus status2=scnsW25qxxTestMemoryMapped(SCNS_W25QXX_QSPI,0x90000000);
    __set_BASEPRI(basePriBak);
    if(status1!=SCNS_STATUS_OK&&status2!=SCNS_STATUS_OK)
    {
        hxPrintfFL("status1=%d:%s\n",status1,scnsStatusGetString(status1));
        hxPrintfFL("status2=%d:%s\n",status2,scnsStatusGetString(status2));
    }
}

#endif
#if defined(SCNS_SMCTP_EXT_OSCILLOSCOPE_ENABLE)&&SCNS_SMCTP_EXT_OSCILLOSCOPE_ENABLE==1

static void vofaTest(void)
{
    scnsSmctpExtOscilloscopeTest(SCNS_SMCTP_CHANNEL_VISUAL_SCOPE,100,0);
}

#endif

__attribute__((noinline)) void test(void)
{
#if defined(SCNS_SMCTP_EXT_OSCILLOSCOPE_ENABLE)&&SCNS_SMCTP_EXT_OSCILLOSCOPE_ENABLE==1
    scnsMenuAddFunction(0,0,vofaTest);
#endif
#ifdef FLASH_TEST
    scnsMenuAddFunction(0,0,flashTest);
#endif
#ifdef MEM_TEST
    scnsMenuAddFunction(0,0,memTest);
#endif
#ifdef SPI_FLASH_TEST
    scnsMenuAddFunction(0,0,spiFlashTest);
#endif
#ifdef QSPI_FLASH_TEST
    scnsMenuAddFunction(0,0,qspiFlashTest);
#endif
#if defined(SCNS_MALLOC_ENABLE)&&SCNS_MALLOC_ENABLE==1
    scnsMenuAddFunction(0,0,scnsMallocPrinDetail);
#endif
#if __has_include("image.h")
    {
        volatile uint64 t=scnsTimerGetUs();
        display();
        hxPrintfFL("%llu us\n",(scnsTimerGetUs()-t));
    }
#endif
    scnsLcdFill(SCNS_LCD_LAYER_AUTO,(Point){0,40},20,20,scnsLcdColorScreenReplaceA(SCNS_LCD_COLOR_WHITE,255),1);
    scnsLcdFill(SCNS_LCD_LAYER_AUTO,(Point){20,40},20,20,scnsLcdColorScreenReplaceA(SCNS_LCD_COLOR_WHITE,128),1);
    scnsLcdFill(SCNS_LCD_LAYER_AUTO,(Point){40,40},20,20,scnsLcdColorScreenReplaceA(SCNS_LCD_COLOR_WHITE,64),1);
    scnsLcdFill(SCNS_LCD_LAYER_AUTO,(Point){160+0,60},20,20,scnsLcdColorScreenReplaceA(SCNS_LCD_COLOR_WHITE,255),1);
    scnsLcdFill(SCNS_LCD_LAYER_AUTO,(Point){160+20,60},20,20,scnsLcdColorScreenReplaceA(SCNS_LCD_COLOR_WHITE,128),1);
    scnsLcdFill(SCNS_LCD_LAYER_AUTO,(Point){160+40,60},20,20,scnsLcdColorScreenReplaceA(SCNS_LCD_COLOR_WHITE,64),1);
    scnsLcdDisplayString(SCNS_LCD_LAYER_AUTO,(Point){160+0,80},"LCD readback and\ntransparency test",&scnsLcdFontConsolas24,SCNS_LCD_COLOR_RED,0,1);
}
